package org.zkoss.zktc.unittest;

import java.util.ArrayList;
import java.util.List;

import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.WebElement;
import org.zkoss.zktc.core.junit.TestCaseBase;
import org.zkoss.zktc.core.widget.Widget;
import org.zkoss.zktc.core.widget.WidgetDriver;

public class SelectorTestCase extends TestCaseBase{


	protected String getTestCaseUrl(){
		return getTestCaseUrl("/unit/selector.zul");
	}
	
	@Test
	public void testSelectElement(){
		WidgetDriver driver = getDriver();
		navigate(getTestCaseUrl());
		
		WebElement elm = findElement(".z-window-embedded");
		Assert.assertNotNull(elm);
		Assert.assertEquals("zktc_1",elm.getAttribute("id"));
		List<WebElement> list = findElements(".z-window-embedded");
		Assert.assertEquals(4, list.size());
		List<String> idlist = new ArrayList<String>();
		for(WebElement em : list){
			idlist.add(em.getAttribute("id"));
		}
		Assert.assertArrayEquals(new String[]{"zktc_1","zktc_3","zktc_5","zktc_7"}, idlist.toArray());
		
		list = findElements(".z-window-embedded-tl");
		Assert.assertEquals(4, list.size());
		idlist = new ArrayList<String>();
		for(WebElement em : list){
			idlist.add(em.getAttribute("id"));
		}
		Assert.assertArrayEquals(new String[]{"","","",""}, idlist.toArray());
	}
	
	@Test
	public void testSelectElementInWidget(){
		WidgetDriver driver = getDriver();
		navigate(getTestCaseUrl());
		
		Widget wd = findWidget(".z-window-embedded");
		Assert.assertNotNull(wd);
		Assert.assertEquals("w1",wd.getId());
		Assert.assertEquals("zktc_1",wd.getUuid());
		
		WebElement elm = wd.findElement(".z-window-embedded");
		Assert.assertNotNull(elm);
		Assert.assertEquals("zktc_3",elm.getAttribute("id"));
		List<WebElement> list = wd.findElements(".z-window-embedded");
		Assert.assertEquals(2, list.size());
		List<String> idlist = new ArrayList<String>();
		for(WebElement em : list){
			idlist.add(em.getAttribute("id"));
		}
		Assert.assertArrayEquals(new String[]{"zktc_3","zktc_5"}, idlist.toArray());
		
		list = wd.findElements(".z-window-embedded-tl");
		Assert.assertEquals(3, list.size());
		idlist = new ArrayList<String>();
		for(WebElement em : list){
			idlist.add(em.getAttribute("id"));
		}
		
		Assert.assertArrayEquals(new String[]{"","",""}, idlist.toArray());
		
		wd = findWidget("$w2");
		Assert.assertNotNull(wd);
		Assert.assertEquals("w2",wd.getId());
		Assert.assertEquals("zktc_3",wd.getUuid());
		
		elm = wd.findElement(".z-window-embedded");
		Assert.assertNotNull(elm);
		Assert.assertEquals("zktc_5",elm.getAttribute("id"));
		list = wd.findElements(".z-window-embedded");
		Assert.assertEquals(1, list.size());
		idlist = new ArrayList<String>();
		for(WebElement em : list){
			idlist.add(em.getAttribute("id"));
		}
		Assert.assertArrayEquals(new String[]{"zktc_5"}, idlist.toArray());

	}
	
	@Test
	public void testSelectWidget(){
		WidgetDriver driver = getDriver();
		navigate(getTestCaseUrl());
		
		Widget wd = findWidget(".z-window-embedded");
		Assert.assertNotNull(wd);
		Assert.assertEquals("w1",wd.getId());
		Assert.assertEquals("zktc_1",wd.getUuid());
		
		
		wd = findWidget("@window");
		Assert.assertNotNull(wd);
		Assert.assertEquals("w1",wd.getId());
		Assert.assertEquals("zktc_1",wd.getUuid());
		
		wd = findWidget("$w1");
		Assert.assertNotNull(wd);
		Assert.assertEquals("w1",wd.getId());
		Assert.assertEquals("zktc_1",wd.getUuid());
		
		wd = findWidget("$w2");
		Assert.assertNotNull(wd);
		Assert.assertEquals("w2",wd.getId());
		Assert.assertEquals("zktc_3",wd.getUuid());
		
		List<String> idlist = new ArrayList<String>();
		for(Widget we : findWidgets(".z-window-embedded")){
			idlist.add(we.getId());
			idlist.add(we.getUuid());
		}
		
		Assert.assertArrayEquals(new String[]{"w1","zktc_1","w2","zktc_3","w3","zktc_5","w4","zktc_7"}, idlist.toArray());
		
		idlist = new ArrayList<String>();
		for(Widget we : findWidgets("@window")){
			idlist.add(we.getId());
			idlist.add(we.getUuid());
		}
		Assert.assertArrayEquals(new String[]{"w1","zktc_1","w2","zktc_3","w3","zktc_5","w4","zktc_7"}, idlist.toArray());
		
		
		Assert.assertNotNull(findWidget(".z-window-embedded-tl"));
		idlist = new ArrayList<String>();
		for(Widget we : findWidgets(".z-window-embedded-tl")){
			idlist.add(we.getId());
			idlist.add(we.getUuid());
		}
		Assert.assertArrayEquals(new String[]{"w1","zktc_1","w2","zktc_3","w3","zktc_5","w4","zktc_7"}, idlist.toArray());
		
		Assert.assertNull(findWidget(".z-window-embedded-tl",true,0));
		Assert.assertEquals(0,findWidgets(".z-window-embedded-tl",true,0).size());
	}
	
	
	@Test
	public void testSelectWidgetInWidget(){
		WidgetDriver driver = getDriver();
		navigate(getTestCaseUrl());
		
		Widget wd = findWidget(".z-window-embedded");
		Assert.assertNotNull(wd);
		Assert.assertEquals("w1",wd.getId());
		Assert.assertEquals("zktc_1",wd.getUuid());
		
		
		Widget cwd = wd.findWidget("@window");
		Assert.assertNotNull(cwd);
		Assert.assertEquals("w1",cwd.getId());
		Assert.assertEquals("zktc_1",cwd.getUuid());
		
		cwd = wd.findWidget("@button");
		Assert.assertNotNull(cwd);
		Assert.assertEquals("btn1",cwd.getLabel());
		Assert.assertEquals("zktc_2",cwd.getUuid());
		
//		cwd = wd.findWidget("$w3");
//		Assert.assertNotNull(cwd);
//		Assert.assertEquals("w3",cwd.getId());
//		Assert.assertEquals("zktc_5",cwd.getUuid());
//		
//		cwd = wd.findWidget("$w1");
//		Assert.assertNull(cwd);
//		
//		cwd = wd.findWidget("$w4");
//		Assert.assertNull(cwd);
		
		List<String> idlist = new ArrayList<String>();
		for(Widget we : wd.findWidgets(".z-window-embedded")){
			idlist.add(we.getId());
			idlist.add(we.getUuid());
		}
		
		Assert.assertArrayEquals(new String[]{"w2","zktc_3","w3","zktc_5"}, idlist.toArray());
		
		
		cwd = wd.findWidget("$w2");
		idlist = new ArrayList<String>();
		for(Widget we : cwd.findWidgets("@window")){
			idlist.add(we.getId());
			idlist.add(we.getUuid());
		}
		Assert.assertArrayEquals(new String[]{"w2","zktc_3","w3","zktc_5"}, idlist.toArray());
		
		
		Assert.assertNotNull(cwd.findWidget(".z-window-embedded-tl"));
		idlist = new ArrayList<String>();
		for(Widget we : cwd.findWidgets(".z-window-embedded-tl")){
			idlist.add(we.getId());
			idlist.add(we.getUuid());
		}
		Assert.assertArrayEquals(new String[]{"w2","zktc_3","w3","zktc_5"}, idlist.toArray());
		
		Assert.assertNull(wd.findWidget(".z-window-embedded-tl",true,0));
		Assert.assertEquals(0,wd.findWidgets(".z-window-embedded-tl",true,0).size());
	}
	
	@Test
	public void testGetParent(){
		WidgetDriver driver = getDriver();
		navigate(getTestCaseUrl());
		
		Widget wd = findWidget("$w3");
		Assert.assertNotNull(wd);
		Assert.assertEquals("w3",wd.getId());
		Assert.assertEquals("zktc_5",wd.getUuid());
		
		wd = wd.getParent();
		Assert.assertNotNull(wd);
		Assert.assertEquals("w2",wd.getId());
		Assert.assertEquals("zktc_3",wd.getUuid());
		
		wd = wd.getParent();
		Assert.assertNotNull(wd);
		Assert.assertEquals("w1",wd.getId());
		Assert.assertEquals("zktc_1",wd.getUuid());
		
		wd = wd.getParent();//page
		Assert.assertNotNull(wd);
		Assert.assertEquals("zk.Page",wd.getAttribute("className"));
		
		wd = wd.getParent();//desktop
		Assert.assertNotNull(wd);
		
		
		wd = wd.getParent();
		Assert.assertNull(wd);
		
	}
	
	@Test
	public void testGetChildren(){
		WidgetDriver driver = getDriver();
		navigate(getTestCaseUrl());
		
		Widget wd = findWidget("$w1");
		Assert.assertNotNull(wd);
		Assert.assertEquals("zul.wnd.Window",wd.getClassName());
		Assert.assertEquals("window",wd.getWidgetName());
		
		Assert.assertEquals("w1",wd.getId());
		Assert.assertEquals("zktc_1",wd.getUuid());
		
		
		List<Widget> children = wd.getChildren();
		Assert.assertEquals(2, children.size());
		
		Assert.assertEquals("btn1",children.get(0).getLabel());
		Assert.assertEquals("zktc_2",children.get(0).getUuid());
		Assert.assertEquals("w2",children.get(1).getId());
		Assert.assertEquals("zktc_3",children.get(1).getUuid());
		
		
		children = children.get(1).getChildren();
		Assert.assertEquals(2, children.size());
		Assert.assertEquals("btn2",children.get(0).getLabel());
		Assert.assertEquals("zktc_4",children.get(0).getUuid());
		Assert.assertEquals("w3",children.get(1).getId());
		Assert.assertEquals("zktc_5",children.get(1).getUuid());
		
		children = children.get(1).getChildren();
		Assert.assertEquals(1, children.size());
		Assert.assertEquals("btn3",children.get(0).getLabel());
		Assert.assertEquals("zktc_6",children.get(0).getUuid());
		
	}
	
	@Test
	public void testNavigate(){
		WidgetDriver driver = getDriver();
		navigate(getTestCaseUrl());
		
		Widget wd = findWidget("$w1");
		Assert.assertNotNull(wd);
		Assert.assertEquals("zul.wnd.Window",wd.getClassName());
		Assert.assertEquals("window",wd.getWidgetName());
		
		Assert.assertEquals("w1",wd.getId());
		Assert.assertEquals("zktc_1",wd.getUuid());
		
		
		Widget c1 = wd.getFirstChild();
		Widget c2 = wd.getLastChild();
		Assert.assertEquals("btn1",c1.getLabel());
		Assert.assertEquals("zktc_2",c1.getUuid());
		Assert.assertEquals("w2",c2.getId());
		Assert.assertEquals("zktc_3",c2.getUuid());
		
		Assert.assertEquals(c1.getNextSibling(),c2);
		Assert.assertEquals(c2.getPreviousSibling(),c1);
		
		wd = c2;
		c1 = wd.getFirstChild();
		c2 = wd.getLastChild();
		Assert.assertEquals("btn2",c1.getLabel());
		Assert.assertEquals("zktc_4",c1.getUuid());
		Assert.assertEquals("w3",c2.getId());
		Assert.assertEquals("zktc_5",c2.getUuid());
		
		Assert.assertEquals(c1.getNextSibling(),c2);
		Assert.assertEquals(c2.getPreviousSibling(),c1);
		
		wd = c2;
		c1 = wd.getFirstChild();
		c2 = wd.getLastChild();
		Assert.assertEquals("btn3",c1.getLabel());
		Assert.assertEquals("zktc_6",c1.getUuid());
		Assert.assertEquals(c1,c2);
		
	}
}
